import type { cut_propose, coils_cut_order, coils_cut_order_detail, stock_order, arrive_bill, mtl_call } from '@/types/billdata'
import { make_billcode, sum, today } from './common'
import {
  e_code_stage,
  e_material_storage_status,
  e_pick_status,
  e_qc_status,
  e_stock_order_type,
  e_stock_out_status,
  e_unit_type
} from '@/types/enum'
import type { pd_qrcode, t_arrive_option } from '@/types/producedata'
import { useDbStore } from '@/stores/modules/database'
import { list_all_arrive_by_order } from './dataUtils'
export const cut_propose_2_coils_cut_order = (cut_propose: cut_propose) => {
  const dbStore = useDbStore()
  const metal = typeof cut_propose.metal === 'string' ? dbStore.metal.find((r) => r.code === cut_propose.metal) : cut_propose.metal
  const use_coil = dbStore.coils.find((r) => r.barcode === cut_propose.use_coil)
  const result: coils_cut_order = {
    billcode: make_billcode('CMO'),
    billdate: today(),
    closed: false,
    height: cut_propose.height,
    use_coil: use_coil!,
    total_weight: ((cut_propose.height * cut_propose.total_area) / 1000000000) * metal?.density!,
    weight_unit: e_unit_type.千克,
    metal: cut_propose.metal,
    material: use_coil?.material!,
    effective_area: cut_propose.effective_area,
    total_area: cut_propose.total_area,
    total_part: sum(cut_propose.process.map((r) => r.part_qty)),
    total_coil_length: cut_propose.total_coil_length,
    total_over_output: cut_propose.total_over_output,
    total_over_out_area: cut_propose.total_over_output_area,
    total_remainder: cut_propose.total_remainder,
    usage: cut_propose.usage!,
    use_width: cut_propose.width,
    details: []
  }
  cut_propose.process.forEach((r) => {
    const detail: coils_cut_order_detail = {
      part_code: r.part_code,
      part_name: r.part_name,
      use_length: r.coil_length!,
      cut_length: r.cut_length,
      cut_width: r.cut_width,
      all_output: r.part_qty + r.over_output!,
      demand_output: r.part_qty,
      over_output: r.over_output!,
      cut_solution: r.cut_solution,
      direction: r.direction,
      mocode: r.mocode,
      demandcode: r.demandcode,
      cut_pattern: r.cut_pattern!,
      rows: r.cut_rows!,
      cuts_each_even_row: r.cuts_each_even_row!,
      cuts_each_odd_row: r.cuts_each_odd_row!
    }
    result.details.push(detail)
  })
  return result
}

export const order_to_arrive = (order: stock_order, qty: number) => {
  const material = typeof order.material === 'string' ? useDbStore().material.find((r) => r.code === order.material) : order.material
  const arrive: arrive_bill = {
    srcbill: order.billcode,
    billtype: order.billtype,
    billcode: make_billcode('AR'),
    status: e_qc_status.未报检,
    billdate: today(),
    material: order.material,
    supplier: order.supplier,
    arrive_qty: qty,
    unit: order.unit,
    staff: order.staff,
    qualified: 0,
    unqualified: 0,
    stockin_qty: 0,
    special_qty: 0,
    return_qty: 0,
    code_after_qc: false,
    storage_policy: material?.storage_policy.find((r) => r.default)!,
    code_qty: 0,
    reserved_qty: order.reserved_qty
  }
  switch (order.billtype) {
    case e_stock_order_type.采购订单:
      arrive.code_after_qc = material?.pu_code_stage === e_code_stage.检后 && !material?.inspection_free
      break
    case e_stock_order_type.委外收货:
      arrive.code_after_qc = material?.oe_code_stage === e_code_stage.检后 && !material?.inspection_free
      break
    case e_stock_order_type.调入订单:
      arrive.code_after_qc = material?.to_code_stage === e_code_stage.检后 && !material?.inspection_free
      break
    case e_stock_order_type.退货入库:
      arrive.code_after_qc = material?.re_code_stage === e_code_stage.检后 && !material?.inspection_free
      break
  }
  return arrive
}
export const bill_to_qrcode = (option: t_arrive_option) => {
  const result: pd_qrcode[] = []
  for (let i = 0; i < option.code_count; i++) {
    const item: pd_qrcode = {
      qrcode: make_billcode('WL'),
      qty: option.unit_qty,
      version: option.version,
      batchcode: option.billcode,
      status: e_material_storage_status.空码,
      location: '',
      ctrl_method: option.ctrl_method,
      last_stock_in: today(),
      last_stock_out: '',
      arrive_bill: option.billcode,
      print_count: 0,
      material: option.material,
      l: option.l,
      w: option.w,
      h: option.h,
      reserv_by: ''
    }
    result.push(item)
  }
  return result
}

export const require_to_mtlcall = (requrie_bill: {
  reqdate: string
  reqorder: string
  b: string
  loc: string
  d: { require_code: string; qty: number }[]
}) => {
  const result: mtl_call[] = []
  const db = useDbStore()
  requrie_bill.d.forEach((r) => {
    const require = db.require_order.find((s) => s.billcode === r.require_code)!
    result.push({
      batchcode: requrie_bill.b,
      srcbill: r.require_code,
      billtype: require.billtype,
      billcode: make_billcode('CL'),
      status: e_pick_status.开立,
      billdate: today(),
      material: require.material,
      call_qty: r.qty,
      unit: require.unit,
      staff: require.staff,
      loc: requrie_bill.loc,
      reqdate: requrie_bill.reqdate,
      reqorder: requrie_bill.reqorder,
      stockout_qty: 0,
      pick_qty: 0
    })
  })
  return result
}
